Public Class fGMFilter

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        Dim sIP As String = Replace(txtIP.Text, " ", "")

        If Not lstIPs.Items.Contains(sIP) Then
            If System.Net.IPAddress.TryParse(sIP, System.Net.IPAddress.Any) Then
                Dim cols() As String = {"STR_IP", "BOOL_DELETED"}
                Dim vals() As String = {"'" & sIP & "'", "FALSE"}

                If Not DatabaseManager.DB_Account.NewRecord("TBL_GAMEMASTERIPFILTER", cols, vals) Then
                    MsgBox("Database error!", MsgBoxStyle.Critical Or MsgBoxStyle.OkOnly)
                Else
                    lstIPs.Items.Add(sIP)
                    lstIPs.SetItemCheckState(lstIPs.Items.Count - 1, CheckState.Checked)
                    txtIP.Text = ""
                End If

            Else
                MsgBox(sIP & " is not a valid IP!", MsgBoxStyle.Critical Or MsgBoxStyle.OkOnly)
            End If
        Else
            MsgBox(sIP & " is already in the filter list!", MsgBoxStyle.Critical Or MsgBoxStyle.OkOnly)
        End If

    End Sub

    Private Sub fGMFilter_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown

        Dim rows As ArrayList
        Dim ssql As String = "SELECT STR_IP, BOOL_DELETED FROM TBL_GAMEMASTERIPFILTER;"

        rows = DatabaseManager.DB_Account.ExecuteQuery(ssql)

        lstIPs.Items.Clear()

        If rows.Count > 0 Then
            For i As Integer = 0 To rows.Count - 1

                lstIPs.Items.Add(rows(i)("STR_IP"))
                If rows(i)("BOOL_DELETED") = "False" Then
                    lstIPs.SetItemCheckState(i, CheckState.Checked)
                Else
                    lstIPs.SetItemCheckState(i, CheckState.Unchecked)
                End If

            Next
        End If


    End Sub

    Private Sub lstIPs_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles lstIPs.ItemCheck

        Dim bool_deleted As String

        If e.NewValue = CheckState.Checked Then
            bool_deleted = "False"
        Else
            bool_deleted = "True"
        End If

        Dim ssql = "UPDATE TBL_GAMEMASTERIPFILTER SET BOOL_DELETED='" & bool_deleted & "' WHERE STR_IP='" & lstIPs.Items(e.Index) & "';"

        DatabaseManager.DB_Account.ExecuteNonQuery(ssql)

    End Sub

End Class